﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.Customer.Record;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Contract.SaleOut.Record;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Configuration;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class UpdateBillingInfoServiceCommand : ProviderQueueCommand<IProviderFactory, BilltoInfRecord, ResponseRecord>
    {
        public UpdateBillingInfoServiceCommand(IProviderFactory provider) : base(provider) { }
        protected override void Validate(BilltoInfRecord record)
        {
            if (record == null) throw new ArgumentNullException("request");
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var billingInfo = Record;
            var obj = new BilltoInf()
            {
                Address = billingInfo.Address,
                Avtive = billingInfo.Avtive.HasValue ? billingInfo.Avtive.Value.ToBooleanString() : null,
                BilltoCode = billingInfo.BilltoCode,
                BizNo = billingInfo.BizNo,
                BuyingGroup = billingInfo.BuyingGroup,
                CreditLevel = billingInfo.CreditLevel,
                CustomerCode = billingInfo.CustomerCode,
                FullName = billingInfo.FullName,
                Grade = billingInfo.Grade,
                HighPriceGroup = billingInfo.HighPriceGroup,
                HighSaleChannel = billingInfo.HighSaleChannel,
                LowPriceGroup = billingInfo.LowPriceGroup,
                LowSaleChannel = billingInfo.LowSaleChannel,
                PayFrom = billingInfo.PayFrom,
                Profile = billingInfo.Profile,
                Representative = billingInfo.Representative,
                ShortName = billingInfo.ShortName,
                TaxId = billingInfo.TaxId,
                VnDepartment = billingInfo.VnDepartment,
                SoTransferDate = DateTime.Now,
                SoTransferFlag = BooleanString.TRUE,
            };
            var itemCheck = Providers.BilltoInf.Retrieve(Record.BilltoCode);
            if (itemCheck == null)
            {
                var resultitem1 = Providers.BilltoInf.Add(obj);
            }
            else
            {
                var resultitem2 = Providers.BilltoInf.Update(obj);
            }
            return true;
        }
    }
}
